package com.guli.gulimall.gulimallcoupon.service.impl;

import com.guli.gulimall.gulimallcoupon.entity.MemberPriceEntity;
import com.guli.gulimall.gulimallcoupon.entity.SkuLadderEntity;
import com.guli.gulimall.gulimallcoupon.service.MemberPriceService;
import com.guli.gulimall.gulimallcoupon.service.SkuLadderService;
import com.guli.to.MemberPrice;
import com.guli.to.SkureductionTo;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.gulimallcoupon.dao.SkuFullReductionDao;
import com.guli.gulimall.gulimallcoupon.entity.SkuFullReductionEntity;
import com.guli.gulimall.gulimallcoupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;
   @Autowired
   private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkureduction(SkureductionTo skureductionTo) {
//        private  Long skuId;
//        private int fullCount;
//        private double discount;
//        private BigDecimal countStatus;
//        private BigDecimal fullPrice;
//        private BigDecimal reducePrice;
//        private BigDecimal priceStatus;
//        private List<MemberPrice> memberPrice;
        //5.4 sku的优惠、满减等信息sms->sku_laddder/sku_full_reduction/memer_price
        //1保存满减打折，会员价
        System.out.println(skureductionTo);
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skureductionTo.getSkuId());
        skuLadderEntity.setFullCount(skureductionTo.getFullCount());
        skuLadderEntity.setDiscount(skureductionTo.getDiscount());
        skuLadderEntity.setAddOther(skureductionTo.getCountStatus());
        skuLadderService.save(skuLadderEntity);
        //sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skureductionTo,skuFullReductionEntity);
        if(skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            System.out.println(skuFullReductionEntity);
            this.save(skuFullReductionEntity);
        }
        //member_price
//        private Long id;
//        private String name;
//        private BigDecimal price;
        List<MemberPrice> memberPriceList = skureductionTo.getMemberPrice();

        List<MemberPriceEntity> memberPriceEntityList = memberPriceList.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skureductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item-> item.getMemberPrice().compareTo(new BigDecimal("0"))==1).collect(Collectors.toList());
        System.out.println(memberPriceEntityList);
        this.memberPriceService.saveBatch(memberPriceEntityList);

    }

}